﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 将xaml文件加载到窗体
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private Button myButton;
        public MainWindow()
        {
            InitializeComponent();
        }

        public MainWindow(string xamlFile)
        {
            this.Height = this.Width = 350;
            this.Left = this.Top = 50;
            this.Title = "Loaded Xaml.";

            //主要是把.xaml文件描述的窗体加载到创建的窗体中
            DependencyObject rootElement;

            using (FileStream fs = new FileStream(xamlFile, FileMode.Open))
            {
                rootElement = (DependencyObject)System.Windows.Markup.XamlReader.Load(fs);//(DependencyObject)XamlReader.Load(fs);

            }
            this.Content = rootElement;

            myButton = (Button)LogicalTreeHelper.FindLogicalNode(rootElement, "button1");
            myButton.Click += myButton_Click;
        }

        private void myButton_Click(object sender,RoutedEventArgs e)
        {
            myButton.Content = "clicked!";
        }
    }
}
